/* James McTaggart (jcm28) Informatics
Multimedia Communications Technology - 17th May 2007
In making this submission I declare that my work contains no examples
of misconduct, such as plagiarism, collusion, or fabrication of results.
*/

package server;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Hashtable;
import java.util.TimerTask;

public class RadioTimerTask extends TimerTask {

	private int port;
	private InetAddress ipAddress;
	private static Hashtable<Integer, String> messages;
	private int counter;
	private DatagramSocket server;
	
	static{
		messages = new Hashtable<Integer, String>();
		messages.put(0,"One");
		messages.put(1,"Two Two");
		messages.put(2,"Three Three Three");
		messages.put(3,"Four Four Four Four");
		messages.put(4,"Five Five Five Five Five");
		messages.put(5,"Six Six Six Six Six Six");
		messages.put(6,"Seven Seven Seven Seven Seven Seven Seven");
		messages.put(7,"Eight Eight Eight Eight Eight Eight Eight Eight");
		messages.put(8,"Nine Nine Nine Nine Nine Nine Nine Nine Nine");
		messages.put(9,"Ten Ten Ten Ten Ten Ten Ten Ten Ten Ten");
	}
	
	public RadioTimerTask(int port, InetAddress ipAddress) {
		this.port = port;
		this.ipAddress = ipAddress;
		//init();
		counter = 0;
		try {
			server = new DatagramSocket();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public String getNextSample(){
		return messages.get(counter++%10);
	}
	
	public static String decodeMessageIDForRetransmit(int messageID){
		String result = "#";
		int n = messageID%10;
		for(int i = 0; i < n; i++)result += (i==(n-1))?n+"#":n+" ";
		return result;
	}
	@Override
	public void run() {
		//This will send packets with data sections as follows;
		//"401SAMP######" have 13 bytes for the header 
		//then upto 47 for the body
		try {
			server.send(ServerProtocol.get401(getNextSample(),counter-1,ipAddress,port));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
 
	public static void main(String[] args) {
		RadioTimerTask t = new RadioTimerTask(1,null);
		System.out.println(t.getNextSample());
	}
}
